package com.raycloud.service;

import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.raycloud.constants.SystemConstant;
import com.raycloud.dao.EndProjectDAO;
import com.raycloud.pojo.EndProject;
import com.raycloud.query.EndProjectQuery;
import com.raycloud.util.CmdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.List;

/**
 * Created by donar on 17/4/20.
 */
public class LoadContextService {
    @Autowired
    EndProjectDAO endProjectDAO;
   @PostConstruct
    public void init() throws IOException {
       new Thread(new Runnable() {
           public void run() {
               while (true){
                   try{
                       List<EndProject> endProjects = endProjectDAO.getEndProjectList(new EndProjectQuery());
                       for(EndProject endProject:endProjects){
                           String result = CmdUtil.exec("curl http://127.0.0.1:"+endProject.getTomcatPort());
                           if (result.contains("couldn't connect to host")){
                               endProject.setStatus("OFF");
                           }else{
                                   endProject.setStatus("ON");
                           }
                           endProjectDAO.updateEndProjectByKey(endProject);
                       }
                       Thread.sleep(45000);
                   }catch (Exception e){

                   }
               }
           }
       }).start();
    }
}
